/*
 * contrib/btree_gist/btree_float4.c
 */
#include "postgres.h"

#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/float.h"

typedef struct float4key
{
	float4		lower;
	float4		upper;
} float4KEY;

/*
** float4 操作
*/
PG_FUNCTION_INFO_V1(gbt_float4_compress);
PG_FUNCTION_INFO_V1(gbt_float4_fetch);
PG_FUNCTION_INFO_V1(gbt_float4_union);
PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
PG_FUNCTION_INFO_V1(gbt_float4_consistent);
PG_FUNCTION_INFO_V1(gbt_float4_distance);
PG_FUNCTION_INFO_V1(gbt_float4_penalty);
PG_FUNCTION_INFO_V1(gbt_float4_same);

static bool fc_gbt_float4gt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const float4 *) fc_a) > *((const float4 *) fc_b));
}
static bool fc_gbt_float4ge(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const float4 *) fc_a) >= *((const float4 *) fc_b));
}
static bool fc_gbt_float4eq(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const float4 *) fc_a) == *((const float4 *) fc_b));
}
static bool fc_gbt_float4le(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const float4 *) fc_a) <= *((const float4 *) fc_b));
}
static bool fc_gbt_float4lt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const float4 *) fc_a) < *((const float4 *) fc_b));
}

static int fc_gbt_float4key_cmp(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	float4KEY  *fc_ia = (float4KEY *) (((const Nsrt *) fc_a)->t);
	float4KEY  *fc_ib = (float4KEY *) (((const Nsrt *) fc_b)->t);

	if (fc_ia->lower == fc_ib->lower)
	{
		if (fc_ia->upper == fc_ib->upper)
			return 0;

		return (fc_ia->upper > fc_ib->upper) ? 1 : -1;
	}

	return (fc_ia->lower > fc_ib->lower) ? 1 : -1;
}

static float8 fc_gbt_float4_dist(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return GET_FLOAT_DISTANCE(float4, fc_a, fc_b);
}


static const gbtree_ninfo tinfo =
{
	gbt_t_float4,
	sizeof(float4),
	8,							/* sizeof(gbtreekey8) */
	fc_gbt_float4gt,
	fc_gbt_float4ge,
	fc_gbt_float4eq,
	fc_gbt_float4le,
	fc_gbt_float4lt,
	fc_gbt_float4key_cmp,
	fc_gbt_float4_dist
};


PG_FUNCTION_INFO_V1(float4_dist);
Datum float4_dist(PG_FUNCTION_ARGS)
{
	float4		fc_a = PG_GETARG_FLOAT4(0);
	float4		fc_b = PG_GETARG_FLOAT4(1);
	float4		fc_r;

	fc_r = fc_a - fc_b;
	if (unlikely(isinf(fc_r)) && !isinf(fc_a) && !isinf(fc_b))
		float_overflow_error();

	PG_RETURN_FLOAT4(Abs(fc_r));
}


/**************************************************
 * float4 操作
 **************************************************/


Datum gbt_float4_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_compress(fc_entry, &tinfo));
}

Datum gbt_float4_fetch(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_fetch(fc_entry, &tinfo));
}

Datum gbt_float4_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	float4		fc_query = PG_GETARG_FLOAT4(1);
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	float4KEY  *fc_kkk = (float4KEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	/* 由此函数服务的所有案例都是精确的 */
	*fc_recheck = false;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_BOOL(gbt_num_consistent(&fc_key, (void *) &fc_query, &fc_strategy,
									  GIST_LEAF(fc_entry), &tinfo,
									  fcinfo->flinfo));
}


Datum gbt_float4_distance(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	float4		fc_query = PG_GETARG_FLOAT4(1);

	/* Oid		subtype = PG_GETARG_OID(3); */
	float4KEY  *fc_kkk = (float4KEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_FLOAT8(gbt_num_distance(&fc_key, (void *) &fc_query, GIST_LEAF(fc_entry),
									  &tinfo, fcinfo->flinfo));
}


Datum gbt_float4_union(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	void	   *fc_out = palloc(sizeof(float4KEY));

	*(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
	PG_RETURN_POINTER(gbt_num_union((void *) fc_out, fc_entryvec, &tinfo, fcinfo->flinfo));
}


Datum gbt_float4_penalty(PG_FUNCTION_ARGS)
{
	float4KEY  *fc_origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	float4KEY  *fc_newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *fc_result = (float *) PG_GETARG_POINTER(2);

	penalty_num(fc_result, fc_origentry->lower, fc_origentry->upper, fc_newentry->lower, fc_newentry->upper);

	PG_RETURN_POINTER(fc_result);
}

Datum gbt_float4_picksplit(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
										(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
										&tinfo, fcinfo->flinfo));
}

Datum gbt_float4_same(PG_FUNCTION_ARGS)
{
	float4KEY  *fc_b1 = (float4KEY *) PG_GETARG_POINTER(0);
	float4KEY  *fc_b2 = (float4KEY *) PG_GETARG_POINTER(1);
	bool	   *fc_result = (bool *) PG_GETARG_POINTER(2);

	*fc_result = gbt_num_same((void *) fc_b1, (void *) fc_b2, &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_result);
}
